package server.controller;

import java.util.Timer;
import java.util.TimerTask;

import l1j.server.server.model.Broadcaster;
import l1j.server.server.model.Instance.L1DollInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SkillSound;

public class DollHpMpRegenController extends TimerTask {
	private static Timer _dollHpMpTimer = new Timer();
	private final int _regenTime;

	public DollHpMpRegenController(int regenTime) {
		_regenTime = regenTime;
	}

	public void start() {
		_dollHpMpTimer.scheduleAtFixedRate(DollHpMpRegenController.this,
				_regenTime, _regenTime);
	}

	@Override
	public void run() {
		/*
		 * try {
		 * 
		 * for(L1PcInstance pc : L1World.getInstance().getDollPlaylist()){ if(pc
		 * == null || pc.isDead() || pc.getNetConnection() == null){ continue;
		 * }else{ for(L1DollInstance doll : pc.getDollList().values()){
		 * if(doll.isHpRegeneration()) regenHp(pc); else
		 * if(doll.isMpRegeneration()) regenMp(pc); } } } } catch(Exception e) {
		 * _log.log(Level.SEVERE, e.getLocalizedMessage(), e); }
		 */

	}

	public void regenMp(L1PcInstance _pc) {
		int regenMp = 0;

		for (L1DollInstance doll : _pc.getDollList().values()) {
			regenMp = doll.getMpRegenerationValues();
		}

		int newMp = _pc.getCurrentMp() + regenMp;

		_pc.setCurrentMp(newMp);
		_pc.sendPackets(new S_SkillSound(_pc.getId(), 6321));
		Broadcaster.broadcastPacket(_pc, new S_SkillSound(_pc.getId(), 6321));

	}

	public void regenHp(L1PcInstance _pc) {

		int newHp = _pc.getCurrentHp() + 40;
		if (newHp < 0) {
			newHp = 0;
		}
		_pc.setCurrentHp(newHp);
		_pc.sendPackets(new S_SkillSound(_pc.getId(), 1608));
		Broadcaster.broadcastPacket(_pc, new S_SkillSound(_pc.getId(), 1608));
	}

}
